package convert

import (
	"github.com/samber/lo"
	"k9-panel/app/adapter/internal"
	"k9-panel/app/model"
	"k9-panel/app/service/dto"
)

func ConfigDataParam2DerivedAddParam(config *internal.ConfigDataParam) (*dto.DerivedAddParam, error) {
	param := dto.DerivedAddParam{}
	param.Content = config.Content
	param.ProjectId = uint(config.ProjectId)
	param.EnvironmentId = uint(config.EnvironmentId)
	param.DependencyProjectIds = config.DependencyProjectIds
	return &param, nil
}
func RspListHistory2HistoryRsp(history *dto.RspListHistory) internal.HistoryRsp {
	data := lo.Map(history.Items, func(item *model.HistoryConfig, _ int) *internal.HistoryData {
		return &internal.HistoryData{
			Id:            item.ID,
			Content:       item.File,
			Memo:          item.Memo,
			EnvironmentId: item.EnvironmentId,
			Status:        item.Status,
			CreatedAt:     item.CreatedAt,
		}
	})
	return internal.HistoryRsp{
		Page: internal.Page{
			Total: history.Total,
		},
		Items: data,
	}
}

func UserTokenList2UserTokenRsp(items []*model.UserToken) []*internal.UserTokenRsp {
	return lo.Map(items, func(item *model.UserToken, index int) *internal.UserTokenRsp {
		return &internal.UserTokenRsp{
			Id:         item.ID,
			Desc:       item.Desc,
			Value:      item.Value,
			ProjectIds: item.ManagerProjectIds,
			EnvIds:     item.ManagerEnvIds,
		}
	})
}
func Account2AccountRsp(items []*model.Account) []*internal.AccountRsp {
	return lo.Map(items, func(item *model.Account, index int) *internal.AccountRsp {
		return &internal.AccountRsp{
			Id:        item.ID,
			Username:  item.Username,
			Super:     item.Super,
			CreatedAt: item.CreatedAt,
		}
	})
}
